package com.cz.nongyetongv5.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.activity.ArchiveMapActivity;
import com.cz.nongyetongv5.bean.ArchiveBean;


/**
 * 我的页面
 * Created by zhangliang on 2017/6/26.
 */

public class ArchiveBaseFragment extends Fragment implements View.OnClickListener {

    private TextView tv_ent_name;
    private TextView tv_ent_kind;
    private TextView tv_area;
    private TextView tv_trans;
    private TextView tv_trans_tache;
    private TextView tv_trans_category;
    private TextView tv_licence;
    private TextView tv_capital;
    private TextView tv_address;
    private TextView tv_coordinate;
    private TextView tv_category;
    private TextView tv_scale_amount;
    private TextView tv_sales;
    private TextView tv_employees;
    private TextView tv_members;
    private TextView tv_checker;
    private TextView tv_checker_phone;
    private TextView tv_info;

    private LinearLayout ll_trans;
    private LinearLayout ll_licence;
    private LinearLayout ll_sales;
    private LinearLayout ll_employees;
    private LinearLayout ll_members;
    private LinearLayout ll_checker;
    private LinearLayout ll_checker_phone;
    private LinearLayout ll_info;
    private View view;

    private ImageView iv_map;
    private ArchiveBean archiveBean;


    public static ArchiveBaseFragment newInstance(ArchiveBean bean) {
        ArchiveBaseFragment fragment = new ArchiveBaseFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("bean", bean);
        fragment.setArguments(bundle);
        return fragment;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_archive_base, null);
        archiveBean = (ArchiveBean) getArguments().getSerializable("bean");
        init();
        return view;
    }


    public void init() {
        iv_map = view.findViewById(R.id.iv_map);
        iv_map.setOnClickListener(this);

        tv_ent_name = view.findViewById(R.id.tv_ent_name);

        tv_ent_kind = view.findViewById(R.id.tv_ent_kind);
        tv_area = view.findViewById(R.id.tv_area);
        tv_trans = view.findViewById(R.id.tv_trans);
        tv_trans_tache = view.findViewById(R.id.tv_trans_tache);
        tv_trans_category = view.findViewById(R.id.tv_trans_category);
        tv_licence = view.findViewById(R.id.tv_licence);
        tv_capital = view.findViewById(R.id.tv_capital);
        tv_address = view.findViewById(R.id.tv_address);
        tv_coordinate = view.findViewById(R.id.tv_coordinate);
        tv_category = view.findViewById(R.id.tv_category);
        tv_scale_amount = view.findViewById(R.id.tv_scale_amount);
        tv_sales = view.findViewById(R.id.tv_sales);
        tv_employees = view.findViewById(R.id.tv_employees);
        tv_members = view.findViewById(R.id.tv_members);
        tv_checker = view.findViewById(R.id.tv_checker);
        tv_checker_phone = view.findViewById(R.id.tv_checker_phone);
        tv_info = view.findViewById(R.id.tv_info);


        ll_trans = view.findViewById(R.id.ll_trans);
        ll_licence = view.findViewById(R.id.ll_licence);
        ll_sales = view.findViewById(R.id.ll_sales);

        ll_employees= view.findViewById(R.id.ll_employees);
        ll_members = view.findViewById(R.id.ll_members);
        ll_checker = view.findViewById(R.id.ll_checker);
        ll_checker_phone= view.findViewById(R.id.ll_checker_phone);
        ll_info = view.findViewById(R.id.ll_info);


        setValues();


    }

    private void setValues() {
        if (archiveBean != null) {
            if (archiveBean.getKind() == 2) {
                //合作社
                ll_employees.setVisibility(View.VISIBLE);
                ll_members.setVisibility(View.VISIBLE);
                ll_licence.setVisibility(View.VISIBLE);
                ll_sales.setVisibility(View.VISIBLE);

                tv_licence.setText(archiveBean.getBusiness_license());
                tv_capital.setText(archiveBean.getCapital() + "万元");
                tv_sales.setText(archiveBean.getSales() + "万元");
                tv_employees.setText(archiveBean.getEmployees());
                tv_members.setText(archiveBean.getMembers());
                if (archiveBean.getIs_transportation() == 1) {
                    ll_trans.setVisibility(View.VISIBLE);
                    tv_trans_tache.setText(archiveBean.getTrans_tache_name());
                    tv_trans_category.setText(archiveBean.getTrans_category_name());
                } else {
                    ll_trans.setVisibility(View.GONE);
                }
                if (archiveBean.getInspector_name() == null || archiveBean.getInspector_name().isEmpty()) {
                    ll_checker.setVisibility(View.GONE);
                } else {
                    ll_checker.setVisibility(View.VISIBLE);
                    tv_checker.setText(archiveBean.getInspector_name());
                }
                if (archiveBean.getInspector_phone() == null || archiveBean.getInspector_phone().isEmpty()) {
                    ll_checker_phone.setVisibility(View.GONE);
                } else {
                    ll_checker_phone.setVisibility(View.VISIBLE);
                    tv_checker_phone.setText(archiveBean.getInspector_phone());
                }
            } else if (archiveBean.getKind() == 7) {
                //种养户
                ll_employees.setVisibility(View.GONE);
                ll_members.setVisibility(View.GONE);
                ll_licence.setVisibility(View.GONE);
                ll_sales.setVisibility(View.GONE);
                ll_checker.setVisibility(View.GONE);
                ll_checker_phone.setVisibility(View.GONE);
                if (archiveBean.getIs_transportation() == 1) {
                    ll_trans.setVisibility(View.VISIBLE);
                    tv_trans_tache.setText(archiveBean.getTrans_tache_name());
                    tv_trans_category.setText(archiveBean.getTrans_category_name());
                } else {
                    ll_trans.setVisibility(View.GONE);
                }

            } else {
                ll_employees.setVisibility(View.VISIBLE);
                ll_members.setVisibility(View.GONE);
                ll_licence.setVisibility(View.VISIBLE);
                ll_sales.setVisibility(View.VISIBLE);


                tv_licence.setText(archiveBean.getBusiness_license());
                tv_capital.setText(archiveBean.getCapital() + "万元");
                tv_sales.setText(archiveBean.getSales() + "万元");
                tv_employees.setText(archiveBean.getEmployees());

                if (archiveBean.getIs_transportation() == 1) {
                    ll_trans.setVisibility(View.VISIBLE);
                    tv_trans_tache.setText(archiveBean.getTrans_tache_name());
                    tv_trans_category.setText(archiveBean.getTrans_category_name());
                } else {
                    ll_trans.setVisibility(View.GONE);
                }
                if (archiveBean.getInspector_name() == null || archiveBean.getInspector_name().isEmpty()) {
                    ll_checker.setVisibility(View.GONE);
                } else {
                    ll_checker.setVisibility(View.VISIBLE);
                    tv_checker.setText(archiveBean.getInspector_name());
                }
                if (archiveBean.getInspector_phone() == null || archiveBean.getInspector_phone().isEmpty()) {
                    ll_checker_phone.setVisibility(View.GONE);
                } else {
                    ll_checker_phone.setVisibility(View.VISIBLE);
                    tv_checker_phone.setText(archiveBean.getInspector_phone());
                }
            }
            tv_ent_name.setText(archiveBean.getName());
            tv_ent_kind.setText(archiveBean.getKind_name());
            tv_area.setText(archiveBean.getRegion_code_name());
            tv_trans.setText(archiveBean.getIs_transportation_name());

            tv_address.setText(archiveBean.getAddress());
            tv_coordinate.setText(archiveBean.getLongitude() + "," + archiveBean.getLatitude());
            tv_category.setText(archiveBean.getCategory_name());
            tv_scale_amount.setText(archiveBean.getScale_amount() + archiveBean.getScale_group_name());

            if (archiveBean.getIntro() == null || archiveBean.getIntro().isEmpty()) {
                ll_info.setVisibility(View.GONE);
            } else {
                ll_info.setVisibility(View.VISIBLE);
                tv_info.setText(archiveBean.getIntro());
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_map:
                Intent intent = new Intent(getActivity(), ArchiveMapActivity.class);
                intent.putExtra("longitude",archiveBean.getLongitude());
                intent.putExtra("latitude", archiveBean.getLatitude());
                intent.putExtra("zoom", archiveBean.getZoom());
                startActivity(intent);
                break;
        }
    }
}